import 'package:flutter/widgets.dart';
import 'package:photo_frame/net/http_manager.dart';
import 'package:photo_frame/net/model/frame_info_model.dart';
import 'package:photo_frame/utils/a_logger.dart';
import 'package:provider/provider.dart';
FramesViewModel _framesViewModel = FramesViewModel();

class FramesProvider extends ChangeNotifierProvider<FramesViewModel> {
  FramesProvider.value() : super.value(value: _framesViewModel);
}

class FramesViewModel extends ChangeNotifier {
  List<FrameInfoModel>? _frames;

  List<FrameInfoModel>? get frames => _frames;

  Future<void> fetch() {
    return HttpManager.instance.doRequest("/api/getframe").then((e) async {
      if (e.isSuccess) {
        var list = e.data!['frames'] as List<dynamic>;
        List<FrameInfoModel> newFrames = [];
        for (var item in list) {
          newFrames.add(FrameInfoModel.fromJson(item));
        }
        _frames = newFrames;
        ALogger.e("FramesViewModel-fetch : ${_frames?.length}");
        notifyListeners();
      }
    });
  }
}
